package com.zh.ssmshirofreemarker.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zh.ssmshirofreemarker.Utils.ShiroAuthorizationHelper;
import com.zh.ssmshirofreemarker.cache.ShiroCacheManager;
import com.zh.ssmshirofreemarker.entity.User;
import com.zh.ssmshirofreemarker.entity.UserRole;
import com.zh.ssmshirofreemarker.mapper.UserRoleMapper;
import com.zh.ssmshirofreemarker.service.IUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zh.ssmshirofreemarker.service.IUserService;
import com.zh.ssmshirofreemarker.service.RedisService;
import com.zh.ssmshirofreemarker.shiro.MyShiroRealm;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZH
 * @since 2018-07-24
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    IUserService userService;

    @Autowired
    RedisService redisService;

    @Override
    public Set<String> getRolesByUsername(User user) {
        return userRoleMapper.selectRolesByUserName(user);
    }


    /**
     * 代码有点多，但是内容不多。
     * 1.删除roleId (删除roleIds中没有，但是数据库中有的roleId)
     * 2.添加roleId (添加数据库中没有，但是roleIds中有的roleId)
     * @param userId
     * @param roleIds
     * @return
     */
    @Transactional
    @Override
    public boolean bindRoles(Long userId,List<Long> roleIds) {
        List<Long> databaseRoleIdList = userRoleMapper.selectRoleIdsByUserId(userId);
        List<UserRole> addUserRoles = new ArrayList<>();
        List<Long> deleteRoleIds = new ArrayList<>();
        if(roleIds == null){
            return false;
        }
        for(Long i : roleIds){
            if(i != null && !databaseRoleIdList.contains(i)){
                UserRole temp = new UserRole(userId,i);
                addUserRoles.add(temp);
            }
        }
        for(Long i : databaseRoleIdList){
            if(!roleIds.contains(i)){
                deleteRoleIds.add(i);
            }
        }
        if(deleteRoleIds.size() > 0){
            userRoleMapper.delete(new EntityWrapper<UserRole>().in("role_id",deleteRoleIds));
        }
        if(addUserRoles.size() > 0){
            insertBatch(addUserRoles);
        }
        //清空当前用户的权限缓存
        User user = userService.selectOne(new EntityWrapper<User>().eq("user_id",userId));
        ShiroAuthorizationHelper.clearAuthorizationInfo(user.getUsername());
        return true;
    }

    @Override
    public List<String> getUserNamesByRoleId(Long roleId) {   //通过角色id 查找拥有该角色的所有用户名
        List<String> result = null;
        try{
            result = userRoleMapper.selectUserNamesByRoleId(roleId);
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

}
